﻿// $Id$
/*! \file
 * \brief Definition of Northwind.DomainModel.EnterpriseLibrary.OrderMapper.
 *
 * \author $Author$
 * \date $Date$
 * \version $Revision$
 */

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;
using Salamanca.DataAccess.EnterpriseLibrary.Data;

namespace Northwind.DomainModel.EnterpriseLibrary
{



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary />
    ///
    ///////////////////////////////////////////////////////////////////////////

    public partial class OrderMapper:
        DataMapper<Order, Order.PrimaryKey, Order.DataTransfer>,
        IOrderMapper
    {

        /// <summary />
        public OrderMapper(Database database, ICacheManager cacheManager):
            base(database, DomainModelCreator.CreateDefault<Order>(), cacheManager)
        {
            _ParameterBuilder=new Salamanca.DataAccess.Data.ParameterBuilder(database.CreateConnection());
        }

        /// <summary />
        public DomainModelKeyedCollection<Order> FindAll(DataMapperCollection dataMappers)
        {
            return _FindAll(dataMappers);
        }

        /// <summary />
        public DomainModelKeyedCollection<Order> FindByCustomer(Customer customer, DataMapperCollection dataMappers)
        {
            DbCommand[] commands=new DbCommand[1];

            commands[0]=Database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.OrderSelectByCustomer,
                    ParameterBuilder.GetMarker("CustomerID", 1)
                )
            );

            Database.AddParameter(
                commands[0],
                ParameterBuilder.GetName("CustomerID", 1),
                DbType.String,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                customer.GetId().Value
            );

            return Load(commands, dataMappers);
        }

        /// <summary />
        protected override IList<DbCommand> CreateSelectCommands(Order.PrimaryKey key)
        {
            return CreateSelectCommands(Database, ParameterBuilder, key);
        }

        /// <summary />
        protected override IList<DbCommand> CreateDeleteCommands(Order.DataTransfer data)
        {
            return CreateDeleteCommands(Database, ParameterBuilder, (Order.DataTransfer)data);
        }

        /// <summary />
        protected override IList<DbCommand> CreateInsertCommands(Order.DataTransfer data)
        {
            return CreateInsertCommands(Database, ParameterBuilder, (Order.DataTransfer)data);
        }

        /// <summary />
        protected override IList<DbCommand> CreateUpdateCommands(Order.DataTransfer data)
        {
            return CreateUpdateCommands(Database, ParameterBuilder, (Order.DataTransfer)data);
        }

        /// <summary />
        protected override Order.DataTransfer GetDataTransferObject(IList<IDataRecord> records)
        {
          return _GetDataTransferObject(records);
        }

        /// <summary />
        protected override void OnInserted(Order domainModel, IList<DbCommand> commands)
        {
            domainModel.SetId(Utils.GetLastId(Database, commands));

            base.OnInserted(domainModel, commands);
        }

        /// <summary />
        internal static IList<DbCommand> CreateSelectCommands(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Order.PrimaryKey key)
        {
            return new DbCommand[] { _CreateSelectCommand(database, parameterBuilder, key) };
        }

        /// <summary />
        internal static IList<DbCommand> CreateDeleteCommands(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Order.DataTransfer data)
        {
            return new DbCommand[] { _CreateDeleteCommand(database, parameterBuilder, (Order.DataTransfer)data) };
        }

        /// <summary />
        internal static IList<DbCommand> CreateInsertCommands(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Order.DataTransfer data)
        {
            return new DbCommand[] { _CreateInsertCommand(database, parameterBuilder, (Order.DataTransfer)data) };
        }

        /// <summary />
        internal static IList<DbCommand> CreateUpdateCommands(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Order.DataTransfer data)
        {
            return new DbCommand[] { _CreateUpdateCommand(database, parameterBuilder, (Order.DataTransfer)data) };
        }

    }
}
